import java.io.{FileWriter, File, BufferedWriter}
import javax.xml.parsers.{DocumentBuilder, DocumentBuilderFactory}
import org.w3c.dom.{Document, NodeList}
/**
 * User: joseph
 * Email:jinntrance@gmail.com
 */
object Type extends Enumeration{
val GROUPS,PEOPLE,MEMBERS_OF_GROUP,GROUPS_OF_PEOPLE,FOLLOWED = Value
}
import Type._
val db = DocumentBuilderFactory.newInstance.newDocumentBuilder;
//importGroups(db);

//importMembersFromPeople(db);
//importMembersFromGroup(db);
importFollowed(db);
importPeople(db);

def importFollowed(db: DocumentBuilder) {
  val toFile = "/home/joseph/workspace/idea/data/followed.txt";
  val fromFolder = "/home/joseph/workspace/idea/data/d_g_follows/";
  val tagName = "fullpath";
  importData(fromFolder, toFile, db, tagName, FOLLOWED);
}
def importMembersFromPeople(db: DocumentBuilder) {
  val toFile = "/home/joseph/workspace/idea/data/members.txt";
  val fromFolder = "/home/joseph/workspace/idea/data/d_groups/";
  val tagName = "fullpath";
  importData(fromFolder, toFile, db, tagName, GROUPS_OF_PEOPLE);
}
def importMembersFromGroup(db: DocumentBuilder) {
  val toFile = "/home/joseph/workspace/idea/data/members.txt";
  val fromFolder = "/home/joseph/workspace/idea/data/d_g_m_list/";
  val tagName = "fullpath";
  importData(fromFolder, toFile, db, tagName, MEMBERS_OF_GROUP);
}
def importPeople(db: DocumentBuilder) {
  val toFile = "/home/joseph/workspace/idea/data/people.txt";
  val fromFolder = "/home/joseph/workspace/idea/data/d_people/";
  val tagName = "fullpath";
  importData(fromFolder, toFile, db, tagName, PEOPLE);
}
def importGroups(db: DocumentBuilder) {
  val toFile = "/home/joseph/workspace/idea/data/groups.txt";
  val fromFolder = "/home/joseph/workspace/idea/data/d_groups_index/";
  val tagName = "fullpath";
  importData(fromFolder, toFile, db, tagName, GROUPS);
}
def splitToId(uri:String):String={
  var uri_arr = uri.trim.split("/");
  uri_arr(uri_arr.length-1)
}

def importData(fromFolder: String, toFile: String, db: DocumentBuilder, tagName: String, t: Type.Value) {
  val txt = new File(toFile);
  if (!txt.exists) txt.createNewFile;
  val writer = new BufferedWriter(new FileWriter(txt,true));//true--> try to append the file
  val folder = new File(fromFolder);
  if (folder.isDirectory) {
    for (file <- folder.listFiles if file.getName.endsWith(".xml")) {
      var dom:Document=null;
     try{
      dom=db.parse(file);
       }catch{case _ => println("exception catched") }
      if (null != dom){
      val e = dom.getElementsByTagName(tagName);
      val uri=e.item(0).getTextContent.trim
      val id=splitToId(uri)
//      println(uri+"\t"+id)
      t match {
        case GROUPS => {
          val e = dom.getElementsByTagName("num_mem"); //number of members
          var num = "0";
          if (1 == e.getLength) {
            val num_string = e.item(0).getTextContent;
            val start = num_string.lastIndexOf("(");
            val end = num_string.lastIndexOf(")");
            //            println(num_string + ":" + start + "-->" + end);
            if (start >= 0 && start + 1 < end)
              num = num_string.substring(start + 1, end);
          }
          writer.write(id +"\t" + num+ "\n");
        }
        case PEOPLE => {
          val e = dom.getElementsByTagName("g_num"); //number of groups
          val c = dom.getElementsByTagName("num"); //number of contacts
          val num = if (1 != e.getLength) 0 else {
            val num_string = e.item(0).getTextContent.trim;
            val start = num_string.lastIndexOf("(");
            val end = num_string.lastIndexOf(")");
            //            println(num_string + ":" + start + "-->" + end);
            if (start >= 0 && start + 1 < end)
              num_string.substring(start + 1, end)
            else 0
          }
          val c_num= if (1 != c.getLength) 0 else {
            val n_string=c.item(0).getTextContent.trim
            if( n_string.isEmpty) 0 else n_string.substring(2)
          }
          writer.write(id + "\t" + c_num +"\t" + num+ "\n");//id+contacts+groups
        }
        case MEMBERS_OF_GROUP => {
          require (uri.indexOf("members") > 0)
          val g_id = splitToId(uri.substring(0, uri.indexOf("members"))) //group'string id
          val e = dom.getElementsByTagName("mem_url"); //group-people
          for (i <- 0 until e.getLength)
            writer.write(g_id + "\t" + splitToId(e.item(i).getTextContent) + "\n");
        }
        case GROUPS_OF_PEOPLE => {
          require (uri.indexOf("groups") > 0)
          val p_id = splitToId(uri.substring(0, uri.indexOf("groups"))) //people'string id
          val e = dom.getElementsByTagName("group_url"); //group-people
          for (i <- 0 until e.getLength)
            writer.write(splitToId(e.item(i).getTextContent) + "\t" + p_id + "\n");
        }
        case FOLLOWED => {
          require (uri.indexOf("contacts") > 0)
          val my_id = splitToId(uri.substring(0, uri.indexOf("contacts"))) //my id
          val e = dom.getElementsByTagName("php_url"); //followed uri
          for (i <- 0 until e.getLength)
            writer.write(splitToId(e.item(i).getTextContent) + "\t" + my_id + "\n");
        }
      }
    }
   }
  }
  writer.flush();
  writer.close();
}
